package cn.suzhang.designpatterns.singleton;

/**
 * 线程安全的双重锁单例模式
 * 
 * @author suzhang
 *
 */
public class Singleton {

	/*
	 * volatile保证每个线程能获取该变量最新值
	 */
	private static volatile Singleton instance;

	/*
	 * 防止通过反射实例化
	 */
	private Singleton() {
		if (null != instance) {
			throw new IllegalStateException("instance already exists");
		}
	}

	public static Singleton getInstance() {

		if (instance == null) {
			synchronized (Singleton.class) {
				if (instance == null) {
					return new Singleton();
				}
			}
		}
		return instance;
	}

}
